var exceptionManagement = {
    addressParams:{},// 地址接口入参
    // 查询列表接口
    params: {
        parentCustomerCode  :'',//渠道商名称
        customerCode:'',//客户名称
        abnormalType:'',//异常类型
        dealStatus:'',//处理状态
        channelOrderId:'',// 对方订单号
        pageNum:1,
        pageSize:20,
        provinceCode:'',//省
        cityCode:'',//市
        areaCode:'',// 区
        townCode:'',// 县
    },
    init: function () {
        this.addClick();
        this.getListsData();
        this.getLowerAddressInfo()
    },
    addClick: function () {
        // 所属渠道模糊查询
        $('#channelId').on('input',function(){
            var value = $('#channelId').val();
            exceptionManagement.channelInfo(value)
            $('.channelLists').show();
        });
        // 获取所属渠道的某一项内容
        $('.channelLists').delegate('li','click',function(){
            var itemLi = $(this).text();
             $('#channelId').val(itemLi);
            exceptionManagement.params.parentCustomerCode = $(this).attr('data-id');
            $('.channelLists').hide();
        });
        // 客户名称
        $('#customerName').on('input',function(){
            if(!exceptionManagement.params.parentCustomerCode){
                layer.msg('请选择所属渠道',{offset:'t'});
                return ;
            }
            var value = $('#customerName').val();
            exceptionManagement.customerInfo(value)
            $('.customerLists').show();
        });
        // 客户名称某一项
        $('.customerLists').delegate('li','click',function(){
            var itemLi = $(this).text();
            $('#customerName').val(itemLi);
            exceptionManagement.params.customerCode =  $(this).attr('data-id');
            $('.customerLists').hide();
        });
        $(document).on('click',function(){
            $('.channelLists').hide();
            $('.customerLists').hide();
        });
        // 一级地址
        $('#first').change(function(){
            var value = $('#first').val();
            exceptionManagement.addressParams.level = '102';
            exceptionManagement.addressParams.parentCode = value;
            exceptionManagement.params.provinceCode = value;
            $('#third').html('<option value="">请选择三级地址</option>')
            $('#fourth').html('<option value="">请选择四级地址</option>')
            exceptionManagement.getLowerAddressInfo();
        });
        // 二级地址
        $('#second').change(function(){
            var value = $('#second').val();
            exceptionManagement.addressParams.level = '103';
            exceptionManagement.addressParams.parentCode = value;
            exceptionManagement.params.cityCode = value;
            $('#fourth').html('<option value="">请选择四级地址</option>')
            exceptionManagement.getLowerAddressInfo();
        });
        // 三级地址
        $('#third').change(function(){
            var value = $('#third').val();
            exceptionManagement.addressParams.level = '104';
            exceptionManagement.addressParams.parentCode = value;
            exceptionManagement.params.areaCode = value;
            exceptionManagement.getLowerAddressInfo();
        });
        //四级地址
        $('#fourth').change(function(){
            var value = $('#fourth').val();
            exceptionManagement.addressParams.parentCode = value;
            exceptionManagement.params.townCode = value;
        });
        // 查询
        $('#searchButton').unbind().on('click',function(){
            exceptionManagement.params.abnormalType = $('#reason').find('option:selected').val();// 异常原因
            exceptionManagement.params.dealStatus = $('#dealStatus').find('option:selected').val();// 处理状态
            exceptionManagement.params.channelOrderId = $('#orderCode').val();//对方订单号
            exceptionManagement.getListsData();
        })
        // 重置
        $('#resetButton').unbind().on('click',function(){
            exceptionManagement.params.parentCustomerCode = '';
            exceptionManagement.params.customerCode = '';
            exceptionManagement.params.abnormalType = '';
            exceptionManagement.params.channelOrderId = '';
            exceptionManagement.params.pageNum = 1;
            exceptionManagement.params.pageSize = 20;
            exceptionManagement.params.provinceCode = '';
            exceptionManagement.params.cityCode = '';
            exceptionManagement.params.areaCode = '';
            exceptionManagement.params.townCode = '';
        })

    },
    getListsData: function () {
                var successData = function (data) {
                    if (data.errorCode == 0) {
                        if (data.data.result.length > 0) {
                            exceptionManagement.renderLists(data.data.result)
                            $(".M-box3").pagination({
                                pageCount: data.data.totalPage,
                                jump: true,
                                coping: true,
                                isHide: false,
                                prevContent: '上页',
                                nextContent: '下页',
                                callback: function (api) {
                                    exceptionManagement.params.pageNum = api.getCurrent()
                                    var successData = function (data) {
                                        exceptionManagement.renderLists(data.data.result)
                                    }
                                    return web.doAjax(root.dockingplatform.queryPreOrderAbnormalByPage, 'post', exceptionManagement.params, successData, 1);
                                }
                            })
                        } else {
                            $(".M-box3").pagination({
                                pageCount: 0
                            })
                            exceptionManagement.renderLists(data.data.result)
                        }
                    } else {
                        $.Huimodalalert('数据异常', 2000);
                    }

                }
                return web.doAjax(root.dockingplatform.queryPreOrderAbnormalByPage, 'post', exceptionManagement.params, successData, 1);
    },
    // 渲染列表
    renderLists: function (data) {
        var dataSource = [];
        $.each(data, function (index, item) {
            var obj = {};
            obj.index = index + 1// 序号
            obj.parentCustomerName   = item.parentCustomerName  ; // 所属渠道
            obj.customerName = item.customerName;// 客户名称
            obj.addressMsg = item.addressMsg || '';// 地址
            // obj.channelOrderId = item.channelOrderId;// 对方订单号
            obj.orderCreatedAt = item.orderCreatedAt;// 建单时间
            obj.orderAmount = item.orderAmount; // 订单金额
            obj.abnormalType = item.abnormalType; // 异常原因
            obj.dealStatus = item.dealStatus; // 处理状态
            obj.dealReason = item.dealReason; // 原因
            obj.dealUserName = item.dealUserName; // 处理人
            obj.updatedAt = item.updatedAt; // 处理时间
            obj.orderId = item.orderId;//列表数据唯一标识---跳转处理页面需要
            dataSource.push(obj)
        })
        exceptionManagement.lists(dataSource)
    },
    lists: function (data) {
        var datatable = $("#datatable").dataTable(); // 初始化dataTable，清空上一个值
        datatable.fnClearTable();//清空一下table
        datatable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "order": [[1, "desc"]],
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [5, 10, 25, 50],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据"
            },
            data: data,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        return '<span style="text-align: center;" >' + row.index + '</span>';
                    },
                    "targets": 0
                },
                // 所属渠道
                {
                    "render": function (data, type, row, meta) {
                        return '<span style="text-align: center;"  title="'+row.parentCustomerName +'">' + row.parentCustomerName + '</span>';

                    },
                    "targets": 1
                },
                // 客户名称
                {
                    "render": function (data, type, row, meta) {
                        return '<span style="text-align: center;" title="'+ row.customerName +'">' + row.customerName + '</span>';
                    },
                    "targets": 2
                },
                // 地址
                {
                    "render": function (data, type, row, meta) {
                        return '<span style="text-align: center;" title="'+ row.addressMsg +'">' + row.addressMsg + '</span>';
                    },
                    "targets": 3
                },
                //对方订单号
                // {
                //     "render": function (data, type, row, meta) {
                //         return '<span style="text-align: center;" title="'+ row.channelOrderId +'">' + row.channelOrderId + '</span>';
                //     },
                //     "targets": 3
                // },
                // 建单时间
                {
                    "render": function (data, type, row, meta) {
                        return '<span style="text-align: center;">' + row.orderCreatedAt + '</span>';
                    },
                    "targets": 4
                },
                // 订单金额
                {
                    "render": function (data, type, row, meta) {
                        var orderAmount = ''
                        if(row.orderAmount){
                            orderAmount = row.orderAmount;
                        }
                        return '<span style="text-align: center;">' + orderAmount +'元' + '</span>';
                    },
                    "targets": 5
                },
                //  异常原因
                {
                    "render": function (data, type, row, meta) {
                        var abnormalType = '';
                        if(row.abnormalType == 10){
                            abnormalType = '项目与地址未匹配'
                        } else if (row.abnormalType == 20) {
                            abnormalType = '项目与项目未匹配'
                        }
                        return '<span style="text-align: center;">' + abnormalType + '</span>';
                    },
                    "targets": 6
                },
                // 处理状态
                {
                    "render": function (data, type, row, meta) {
                        //dealStatus: 处理结果:0=未处理;10=>已处理;20=>无需处理
                        var dealStatus = '';
                        if(row.dealStatus == 0){
                            dealStatus = '待处理'
                        }else if(row.dealStatus == 10){
                            dealStatus = '已处理'
                        }else if(row.dealStatus == 20){
                            dealStatus = '无需处理'
                        }
                        return '<span style="text-align: center;">' + dealStatus + '</span>';
                    },
                    "targets": 7
                },
                // 原因
                {
                    "render": function (data, type, row, meta) {

                        return '<span style="text-align: center;" title="'+ row.dealReason +'">' + row.dealReason + '</span>';
                    },
                    "targets": 8
                },
                //处理人
                {
                    "render": function (data, type, row, meta) {
                        return '<span style="text-align: center;">' + row.dealUserName + '</span>';
                    },
                    "targets": 9
                },
                // 处理时间
                {
                    "render": function (data, type, row, meta) {
                        var updatedAt = '';
                        if(row.updatedAt){
                            updatedAt = row.updatedAt;
                        }
                        return '<span style="text-align: center;">' + updatedAt + '</span>';
                    },
                    "targets": 10
                },
                {
                    "render": function (data, type, row, meta) {
                        if(row.dealStatus == 0){
                            return "<span class='operate-btn handle-btn' onclick='exceptionManagement.handleOrder(" + row.orderId +"," + row.abnormalType +")'>处理</span>"
                        }else{
                            return "<span class='operate-btn check-btn' onclick='exceptionManagement.checkOrder(" + row.orderId + "," + row.abnormalType +")'>查看</span>"
                        }

                    },
                    "targets": 11
                },
            ],
        })
    },
    // 处理订单
    handleOrder:function(orderId, abnormalType){
        // 处理订单页面
        var url = layer.open({
            type: 2,
            title: '处理订单',
            content: 'exceptionHandle.html?orderId='+orderId+'&abnormalType='+abnormalType
        });
        layer.full(url);
    },
    // 查看订单
    checkOrder:function(orderId, abnormalType){
        // 处理订单页面
        var url = layer.open({
            type: 2,
            title: '处理订单',
            content: 'exceptionHandle.html?isCheck=true&orderId='+orderId+'&abnormalType='+abnormalType
        });
        layer.full(url);
    },
    // 所属渠道模糊查询接口
    channelInfo:function(value){
        var params = {
            name:value,
        }
        var successData = function(data){
            if(data.errorCode == 0){
                var result = data.data;
                if(result && result.length >0){
                    var channelItem =  '';
                    result.forEach(function(item,index){
                        channelItem +="<li class='clearfix pl-10 mb-5' data-id='"+item.customerCode+"'>"+item.customerName+"</li>"
                    });
                    $('.channelLists').html(channelItem)
                }else{
                    var empty = '<li class="clearfix pl-10 mb-5">无数据</li>';
                    $('.channelLists').html(empty)
                }
            }
        }
        return web.doAjax(root.channelInovice.getChannelConfigByChannelName, 'post', params, successData, 2);
    },
    // 客户名称
    customerInfo:function(value){
        var params = {
            customerName:value,
            parentCustomerCode:exceptionManagement.params.parentCustomerCode
            /*parentCustomerCode:'D000000022'*/
        }
        var successData = function(data){
            if(data.errorCode == 0){
                var result = data.data;
                if(result && result.length >0){
                    var channelItem =  '';
                    result.forEach(function(item,index){
                        channelItem +="<li class='clearfix pl-10 mb-5' data-id='"+item.customerCode+"'>"+item.customerName+"</li>"
                    });
                    $('.customerLists').html(channelItem)
                }else{
                    var empty = '<li class="clearfix pl-10 mb-5">无数据</li>';
                    $('.customerLists').html(empty)
                }
            }
        }
        return web.doAjax(root.dockingplatform.seachSubChanelConfigByName, 'post', params, successData, 1);
    },

    // 地址查询
    getLowerAddressInfo:function(){
        var successCallBack = function(data){
            if(data.errorCode == 0){
                var result = data.list;
                if(exceptionManagement.addressParams.level == '102'){
                    exceptionManagement.renderSecondAddress(result)
                }else if(exceptionManagement.addressParams.level == '103'){
                    exceptionManagement.renderThirdAddress(result)
                }else if(exceptionManagement.addressParams.level == '104'){
                    exceptionManagement.renderFourthAddress(result)
                }else{
                    exceptionManagement.renderFirstAddress(result)
                }

            }
        }
        return  web.doAjax(root.member.getLowerAddress,'post',exceptionManagement.addressParams, successCallBack);
    },
    // 渲染一级地址
    renderFirstAddress:function(data){
        if(data && data.length > 0){
            var str = "<option value=''>请选择一级地址</option>";
            data.forEach(function(item){
                str +='<option value="'+item.code+'">'+item.name+'</option>'
            });
            $('#first').html(str)
        }else{
            var emptyStr = '<option value="">暂无地址</option>';
            $('#first').html(emptyStr)
        }
    },
    // 渲染二级地址
    renderSecondAddress:function(data){
        if(data && data.length > 0){
            var str = "<option value=''>请选择二级地址</option>";
            data.forEach(function(item){
                str +='<option value="'+item.code+'">'+item.name+'</option>'
            });
            $('#second').html(str)
        }else {
            var emptyStr = '<option value="">暂无地址</option>';
            $('#second').html(emptyStr)
        }
    },
    // 渲染三级地址
    renderThirdAddress:function(data){
        if(data && data.length > 0){
            var str = "<option value=''>请选择三级地址</option>";
            data.forEach(function(item){
                str +='<option value="'+item.code+'">'+item.name+'</option>'
            });
            $('#third').html(str)
        }else {
            var emptyStr = '<option value="">暂无地址</option>';
            $('#third').html(emptyStr)
        }
    },
    // 渲染四级地址
    renderFourthAddress:function(data){
        if(data && data.length > 0){
            var str = "<option value=''>请选择四级地址</option>";
            data.forEach(function(item){
                str +='<option value="'+item.code+'">'+item.name+'</option>'
            });
            $('#fourth').html(str)
        }else {
            var emptyStr = '<option value="">暂无地址</option>';
            $('#fourth').html(emptyStr)
        }
    },

}
exceptionManagement.init()